package com.lizhangjun.nim.listener;

import com.alibaba.fastjson.JSON;
import com.lizhangjun.nim.constant.ChatType;
import com.lizhangjun.nim.constant.IMessage;
import com.lizhangjun.nim.constant.SystemConstant;
import com.lizhangjun.nim.session.SessionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.yeauty.pojo.Session;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class Sender {

    private static volatile Sender sender;

    public static Sender getSender() {
        if (sender == null){
            synchronized (Sender.class){
                if (sender == null){
                    sender = new Sender();
                }
            }
        }
        return sender;
    }


    public void send(IMessage IMessage){
        if (IMessage.getChatType().equals(ChatType.BROADCAST.getIndex())){
            //广播
            broadcastSend(IMessage);
        }else if (IMessage.getChatType().equals(ChatType.MULTICAST.getIndex())){
            //组播
            multicastSend(IMessage);
        }else if (IMessage.getChatType().equals(ChatType.UNICAST.getIndex())){
            //单播
            unicastSend(IMessage);
        }else {
            //其他情况
            log.warn("异常消息拦截：" + IMessage.toString());
        }
    }

    /**
     * 广播匿名通道发送
     * @param iMessage 消息体
     */
    private void broadcastSend(IMessage iMessage){
        try {
            List<Session> sessionList = SessionManager.get();
            if (sessionList != null && sessionList.size() > 0){
                for (Session ns : sessionList){
                    if (ns.isOpen()){
                        ns.sendText(JSON.toJSONString(iMessage));
                    }
                }
            }
        }catch (Exception e){
            log.error("broadcastSend异常：", e);
        }
    }

    /**
     * 组播发送
     * @param iMessage 消息体
     */
    private void multicastSend(IMessage iMessage){
        try {
            List<Session> sessionList = SessionManager.get(iMessage.getToGroupId());
            if (sessionList.size() > 0){
                for (Session ns : sessionList){
                    if (ns.isOpen()){
                        ns.sendText(JSON.toJSONString(iMessage));
                    }
                }
            }
        }catch (Exception e){
            log.error("multicastSend异常：", e);
        }
    }

    /**
     * 单播发送
     * @param iMessage 消息体
     */
    private void unicastSend(IMessage iMessage){
        try {
            //获取用户id集合
            String[] split = iMessage.getToUserId().split(SystemConstant.DEFAULT_REGEX);
            List<String> userList = Arrays.asList(split);
            //获取用户连接集合
            List<Session> sessionList = SessionManager.get(userList);
            if (sessionList.size() > 0){
                for (Session ns : sessionList){
                    if (ns.isOpen()){
                        ns.sendText(JSON.toJSONString(iMessage));
                    }
                }
            }
        }catch (Exception e){
            log.error("unicastSend异常：", e);
        }
    }


}
